<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="会员" prop="memberMobile">
        <el-input
          v-model="queryParams.memberMobile"
          placeholder="请输入会员手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="是否已开" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['member:tax:edit']"
        >修改
        </el-button>
      </el-col>

    </el-row>

    <el-table v-loading="loading" :data="taxList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">

          <el-card class="box-card">
            <div class="text item">
              <el-row :gutter="24" class="expand-item">
                <el-col :span="12">公司地址：{{props.row.addr}} </el-col>
                <el-col :span="12">联系电话：{{props.row.mobile}} </el-col>
              </el-row>
              <el-row :gutter="24" class="expand-item">
                <el-col :span="12">开户行：{{props.row.bank}}</el-col>

                <el-col :span="12">银行卡号：{{props.row.bankCard}}</el-col>
              </el-row>
            </div>
          </el-card>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="会员" align="center" prop="memberMobile"/>
      <el-table-column label="金额" align="center" prop="amount"/>
      <el-table-column label="抬头" align="center" prop="invoiceTitle"/>
      <el-table-column label="税号" align="center" prop="taxNum"/>
      <el-table-column label="是否已开" align="center" prop="state" :formatter="stateFormat"/>
      <el-table-column label="邮箱" align="center" prop="email"/>
      <el-table-column label="类型" align="center" prop="type" :formatter="typeFormat"/>
      <el-table-column label="申请时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['member:tax:edit']"
          >修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改开票历史对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入金额" disabled/>
        </el-form-item>
        <el-form-item label="抬头" prop="invoiceTitle">
          <el-input v-model="form.invoiceTitle" placeholder="请输入抬头"/>
        </el-form-item>
        <el-form-item label="税号" prop="taxNum">
          <el-input v-model="form.taxNum" placeholder="请输入税号"/>
        </el-form-item>
        <el-form-item label="是否已开" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态" clearable size="small">
            <el-option
              v-for="dict in stateOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱"/>
        </el-form-item>
<!--        <el-form-item label="类型">-->
<!--          <el-select v-model="form.type" placeholder="请选择类型 1 个人  2 单位">-->
<!--            <el-option label="请选择字典生成" value=""/>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="公司地址" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入公司地址"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="开户行" prop="bank">
          <el-input v-model="form.bank" placeholder="请输入开户行"/>
        </el-form-item>
        <el-form-item label="银行卡号" prop="bankCard">
          <el-input v-model="form.bankCard" placeholder="请输入银行卡号"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listTax, getTax, delTax, addTax, updateTax, exportTax} from "@/api/member/tax";

  export default {
    name: "Tax",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 开票历史表格数据
        taxList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          memberMobile: undefined,
          state: undefined,
          type: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        stateOptions: [],
        typeOptions: []
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_is_enable").then(response => {
        this.stateOptions = response.data;
      });
      this.getDicts("invoice_type").then(response => {
        this.typeOptions = response.data;
      });
    },
    methods: {
      /** 查询开票历史列表 */
      getList() {
        this.loading = true;
        listTax(this.queryParams).then(response => {
          this.taxList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          memberId: undefined,
          amount: undefined,
          invoiceTitle: undefined,
          taxNum: undefined,
          state: undefined,
          email: undefined,
          type: undefined,
          addr: undefined,
          mobile: undefined,
          bank: undefined,
          bankCard: undefined,
          createTime: undefined,
          updateTime: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加开票历史";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getTax(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改开票历史";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateTax(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addTax(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除开票历史编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delTax(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有开票历史数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportTax(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      },
      stateFormat(row, column) {
        return this.selectDictLabel(this.stateOptions, row.state);
      },
      typeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.type);
      },
    }
  };
</script>
